<template>
  <router-view></router-view>
</template>

<script>
import { ibase, os } from "@/utils/base.js";

export default {
  name: "App",
  computed: {},
  data() {
    return {
      wxCode: "",
    };
  },
  created() {
    ibase.init({});
    if (os.weixin) {
      if(window.location.pathname =='/h5/promotion'){
        return;
      }
      this.getUrl();
    }
  },
  methods: {
    getUrl() {
      let userAgent = navigator.userAgent;
      if (userAgent.includes("iPhone") || userAgent.includes("iPad")) {
        sessionStorage.setItem("originUrl", location.href); // 用于ios分享
      }
      this.getBaseInfos();
    },

    // 编码函数
    getUrlParam(name) {
      var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
      var r = window.location.search.substr(1).match(reg); //匹配目标参数

      if (r != null) return unescape(r[2]);
      return null; //返回参数值
    },

    getBaseInfos() {
      if (this.isWeiXin()) {
        const code = this.getUrlParam("code"); // 截取路径中的code
        if (code == null || code === "") {
          let url = "";
          let userAgent = navigator.userAgent;
          // if (userAgent.includes("iPhone") || userAgent.includes("iPad")) {
          //   url = sessionStorage.getItem("originUrl");
          // } else {
          //   url = window.location.href;
          // }
          url = window.location.href;
          // url='https://www.zhishiyisheng.com/h5'
          window.location.href =
            "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx76285fabea037331&redirect_uri=" +
            encodeURIComponent(url) +
            "&response_type=code&scope=snsapi_base&state=1&connect_redirect=1#wechat_redirect";
        } else {
        }
        if (code != "" && code != null) {
          this.wxCode = code;
          sessionStorage.setItem("wxCode", code);
          // this.getOpenid(code);
        }
      } else {
      }
    },

    isWeiXin() {
      var ua = window.navigator.userAgent.toLowerCase();
      if (ua.match(/MicroMessenger/i) == "micromessenger") {
        return true;
      } else {
        return false;
      }
    },
  },
};
</script>
<style lang="scss">
@import "assets/scss/style.scss";
</style>

